<?php

namespace app\api\controller;

use app\common\lib\Time;

class User extends AuthBase
{
    public function index()
    {

        $user = (new \app\common\business\User())->getNormalUserById($this->userId);

        $Users = [
            'id' => $this->userId,
            "username" => $user['username'],
            "sex" => $user['sex'],

        ];
        //返回需要的用户信息给前端
        return show(config("status.success"), "ok", $Users);
    }

    /**
     * 用户数据 更新
     * @return \think\response\Json
     */
    public function update()
    {


        //接收数据
        $userId = input("param.id", "", "intval");
        $username = input("param.username", "", "trim");
        $sex = input("param.sex", 0, "intval");

        $data = [
            'username' => $username,
            'sex' => $sex,
        ];
        //验证数据
        $validate = (new \app\api\validate\User())->scene('update_user');
        if (!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        //更新数据
        $userBisObj = new \app\common\business\User();
        $user = $userBisObj->update($this->userId, $data);
        if (!$user) {
            return show(config('status.error'), "更新失败");
        }
        $redisDate = [
            "id" => $userId,
            "username" => $username,
        ];

        //修改redis数据
        cache(config("redis.token_pre") . $this->accessToken, $redisDate, Time::userLoginExpiresTime($this->type));
        return show(1, 'ok');

    }

}